package ui;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Login extends JFrame {
    private  Font font = new Font("宋体",Font.BOLD, 14);
    private  JLabel message;
    public Login(){
        init();
    }

    //初始化
    private void init() {
        this.setTitle("考试系统-登陆");
        this.add(createContentPanels());
        this.setSize(380,270);
        //居中
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int op=JOptionPane.showConfirmDialog(null,"确定要退出吗？");
                if (JOptionPane.YES_OPTION==op){
                    System.exit(0);
                }
            }
        });
        //this.setDefaultCloseOperation(3);
    }

    private JPanel createContentPanels() {
        JPanel p = new JPanel(new BorderLayout(0,21));
        JLabel title = new JLabel("考试登陆系统", JLabel.CENTER);
        Font font = new Font("宋体",Font.BOLD, 16);
        title.setFont(font);
        p.add(BorderLayout.NORTH,title);
        p.add(BorderLayout.CENTER,createCenterPanel());
        p.add(BorderLayout.SOUTH,createButtomPanel());
        return p;
    }

    private ClientContext client;
    private JPanel createButtomPanel() {
        JPanel p = new JPanel();
        p.setFont(font);
        JButton login = new JButton("登陆");
        JButton cancel = new JButton("取消");
        login.setPreferredSize(new Dimension(80,40));
        cancel.setPreferredSize(new Dimension(80,40));
        p.add(login);
        p.add(cancel);
        login.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                client.login();
            }
        });
        cancel.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int op=JOptionPane.showConfirmDialog(null,"你确定要退出？");
                if (JOptionPane.YES_OPTION==op){
                    System.exit(0);
                }
            }
        });

        return p;
    }

    private JPanel createCenterPanel() {
        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(new EmptyBorder(8,8,8,8));
        p.add(BorderLayout.NORTH,createIdPwdPanel());

        message = new JLabel("", JLabel.CENTER);
        p.add(message, BorderLayout.CENTER);
        return p;
    }

    private JPanel createIdPwdPanel() {
        JPanel p = new JPanel(new GridLayout(2,1,0,12));
        p.add(createIdPanel());
        p.add(createPwdPanel());

        return p;
    }

    private JTextField idField;//私有的id成员变量
    private JPasswordField pwdField;//私有的pwd成员变量
    private JPanel createIdPanel() {
        JPanel p = new JPanel(new BorderLayout(5,0));
        JLabel id = new JLabel("编号：");
        id.setFont(font);

        p.add(BorderLayout.WEST,id);
        JTextField idField = new JTextField();
        p.add(BorderLayout.CENTER, idField);
        this.idField=idField;
        return p;
    }

    private JPanel createPwdPanel() {
        JPanel p = new JPanel(new BorderLayout(5,0));
        JLabel pw = new JLabel("密码：");
        pw.setFont(font);

        p.add(BorderLayout.WEST,pw);
        JPasswordField pwdField = new JPasswordField();
        p.add(BorderLayout.CENTER, pwdField);
        this.pwdField=pwdField;
        return p;
    }

    public int getUserId() {
        String text=idField.getText();
        int id=Integer.parseInt(text);
        idField.setText(null);
        return id;
    }

    public String getUserPwd() {
        char[] ch=pwdField.getPassword();
        pwdField.setText(null);
        return new String(ch);
    }

    public void setClient(ClientContext client) {
        this.client = client;
    }
    public void showError(String message) {
        this.message.setText(message);
    }
}